home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 351-375 / 351 / pdc / pdcsrc.lzh / PDC / Decl.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  26KB  |  1,030 lines

  1.  
  2. /* PDC Compiler - A Freely Distributable C Compiler for the Amiga
  3.  *                Based upon prior work by Matthew Brandt and Jeff Lydiatt.
  4.  *
  5.  * PDC Compiler release 3.3 Copyright (C) 1989 Paul Petersen and Lionel Hummel.
  6.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  7.  *
  8.  * This code is freely redistributable upon the conditions that this 
  9.  * notice remains intact and that modified versions of this file not be 
  10.  * distributed as part of the PDC Software Distribution without the express
  11.  * consent of the copyright holders.
  12.  *
  13.  *------------------------------------------------------------------
  14.  *
  15.  * $Log:    Decl.c,v $
  16.  * Revision 3.33  90/04/04  23:17:41  lionel
  17.  * Fixed parsing of multidimensional arrays.
  18.  * 
  19.  * Revision 3.32  90/02/03  16:23:20  lionel
  20.  * None
  21.  * 
  22.  *------------------------------------------------------------------
  23.  */
  24.  
  25. /*
  26.  * Decl.c
  27.  * 
  28.  * Manages typedef's and variable declarations in the symbol table.
  29.  */
  30.  
  31. #include    <stdio.h>
  32. #include    "C.h"
  33. #include    "Expr.h"
  34. #include    "Gen.h"
  35. #include    "Cglbdec.h"
  36.  
  37. TYP            *head = NULL;
  38. TYP            *tail = NULL;
  39. char           *declid = NULL;
  40. TABLE           tagtable = {NULL, NULL};
  41. TYP             stdconst = {bt_long, 1, 4, {NULL, NULL}, 0, "const"};
  42.  
  43. void    decl2(), declenum(), enumbody(), declstruct(), structbody();
  44.  
  45. extern SYM      *search();
  46. extern long     intexpr();
  47. extern int      fatal;
  48.  
  49. #ifdef  GENERATE_DBX
  50. extern int      dbx_ident();
  51. #endif
  52.  
  53. extern struct snode *asmstmt();
  54. extern char    *xalloc();
  55.  
  56. int
  57. imax(i, j)
  58.     int             i, j;
  59. {
  60.     return (i > j) ? i : j;
  61. }
  62.  
  63. char           *
  64. litlate(s)
  65.     char           *s;
  66. {
  67.     char           *p;
  68.  
  69.     p = (char *) xalloc(strlen(s) + 1);
  70.     strcpy(p, s);
  71.     return p;
  72. }
  73.  
  74. SYM            *
  75. copysym(sp)
  76.     SYM            *sp;
  77. {
  78.     SYM            *esp;
  79.     int             siz;
  80.  
  81.     siz = sizeof(SYM);
  82.     ++global_flag;
  83.     esp = (SYM *) xalloc(siz);
  84.     --global_flag;
  85.  
  86.     if (esp != NULL) {
  87.         *esp = *sp;
  88.         esp->next = NULL;
  89.     }
  90.     return (esp);
  91. }
  92.  
  93.  
  94. TYP            *
  95. maketype(bt, siz)
  96.     enum e_bt       bt;
  97.     int             siz;
  98. {
  99.     TYP            *tp;
  100.  
  101.     tp = (TYP *) xalloc(sizeof(TYP));
  102.     tp->val_flag = 0;
  103.     tp->size = siz;
  104.     tp->type = bt;
  105.     tp->sname = NULL;
  106.     tp->lst.head = NULL;
  107.     tp->lst.tail = NULL;
  108.     return tp;
  109. }
  110.  
  111. TYP            *
  112. istypedef(table)
  113.     TABLE          *table;
  114. {
  115.     SYM            *sp;
  116.  
  117.     if (lastst == id) {
  118.         if (table != NULL) {
  119.             sp = search(lastid, table->head);
  120.             if (sp != NULL && sp->tp != NULL && sp->tp->type == bt_typedef)
  121.                 return (sp->tp->btp);
  122.         }
  123.         sp = search(lastid, lsyms.head);
  124.         if (sp != NULL && sp->tp != NULL && sp->tp->type == bt_typedef)
  125.             return (sp->tp->btp);
  126.         sp = search(lastid, gsyms.head);
  127.         if (sp != NULL && sp->tp != NULL && sp->tp->type == bt_typedef)
  128.             return (sp->tp->btp);
  129.     }
  130.     return (NULL);
  131. }
  132.  
  133. int
  134. is_class_error()
  135. {
  136.     if (lastst == kw_auto) {
  137.         error( ERR_SYNTAX, "auto keyword in wrong position" );
  138.         getsym();
  139.     }
  140.     else if (lastst == kw_const) {
  141.         error( ERR_SYNTAX, "const keyword in wrong position" );
  142.         getsym();
  143.     }
  144.     else if (lastst == kw_static) {
  145.         error( ERR_SYNTAX, "static keyword in wrong position" );
  146.         getsym();
  147.     }
  148.     else if (lastst == kw_register) {
  149.         error( ERR_SYNTAX, "register keyword in wrong position" );
  150.         getsym();
  151.     }
  152. }
  153.  
  154. void
  155. decl(table)
  156.     TABLE          *table;
  157. {
  158.     TYP            *tp;
  159.  
  160.     switch (lastst) {
  161.     case kw_typedef:
  162.         getsym();
  163.         fprintf( stderr, "DIAG -- SHOULD NEVER HAPPEN\n" );
  164.         break;
  165.     case kw_auto:
  166.         getsym();
  167.         decl(table);
  168.         break;
  169.     case kw_const:
  170.         getsym();
  171.         decl(table);
  172.         break;
  173.     case kw_volatile:
  174.         getsym();
  175.         decl(table);
  176.         break;
  177.     case kw_char:
  178.         head = tail = maketype(bt_char, 1);
  179.         getsym();
  180.         is_class_error();
  181.         break;
  182.     case kw_short:
  183.         head = tail = maketype(bt_short, 2);
  184.         getsym();
  185.         if (lastst == kw_int)
  186.             getsym();
  187.         is_class_error();
  188.         break;
  189.     case kw_long:
  190.         head = tail = maketype(bt_long, 4);
  191.         getsym();
  192.         if (lastst == kw_int)
  193.             getsym();
  194.         is_class_error();
  195.         break;
  196.     case kw_int:
  197.     case kw_void:
  198.         head = tail = maketype(bt_long, 4);
  199.         getsym();
  200.         is_class_error();
  201.         break;
  202.     case kw_signed:
  203.         getsym();
  204.         switch (lastst) {
  205.         case kw_char:
  206.             head = tail = maketype(bt_char, 1);
  207.             getsym();
  208.             break;
  209.         case kw_short:
  210.             head = tail = maketype(bt_short, 2);
  211.             getsym();
  212.             break;
  213.         case kw_int:
  214.         case kw_long:
  215.             head = tail = maketype(bt_long, 4);
  216.             getsym();
  217.             break;
  218.         }
  219.         is_class_error();
  220.         break;
  221.     case kw_unsigned:
  222.         getsym();
  223.         switch (lastst) {
  224.         case kw_char:
  225.             head = tail = maketype(bt_uchar, 1);
  226.             getsym();
  227.             break;
  228.         case kw_short:
  229.             head = tail = maketype(bt_ushort, 2);
  230.             getsym();
  231.             break;
  232.         case kw_int:
  233.         case kw_long:
  234.             head = tail = maketype(bt_unsigned, 4);
  235.             getsym();
  236.             break;
  237.         default:
  238.             head = tail = maketype(bt_unsigned, 4);
  239.             break;
  240.         }
  241.         is_class_error();
  242.         break;
  243.     case id:        /* no type declarator, or could be a typedef  */
  244.         if ((tp = istypedef(table)) != NULL) {
  245.             head = tail = tp;
  246.             getsym();
  247.         }
  248.         else
  249.             head = tail = maketype(bt_long, 4);
  250.         break;
  251.     case kw_float:
  252.         head = tail = maketype(bt_float, 4);
  253.         getsym();
  254.         is_class_error();
  255.         break;
  256.     case kw_double:
  257.         head = tail = maketype(bt_double, 8);
  258.         getsym();
  259.         is_class_error();
  260.         break;
  261.     case kw_enum:
  262.         getsym();
  263.         declenum(table);
  264.         is_class_error();
  265.         break;
  266.     case kw_struct:
  267.         getsym();
  268.         declstruct(bt_struct);
  269.         is_class_error();
  270.         break;
  271.     case kw_union:
  272.         getsym();
  273.         declstruct(bt_union);
  274.         is_class_error();
  275.         break;
  276.     }
  277. }
  278.  
  279. void
  280. decl1()
  281. {
  282.     TYP            *temp1, *temp2, *temp3, *temp4;
  283.  
  284.     switch (lastst) {
  285.     case id:
  286.         declid = litlate(lastid);
  287.         getsym();
  288.         decl2();
  289.         break;
  290.     case star:
  291.         temp1 = maketype(bt_pointer, 4);
  292.         temp1->btp = head;
  293.         head = temp1;
  294.         if (tail == NULL)
  295.             tail = head;
  296.         getsym();
  297.         decl1();
  298.         break;
  299.     case openpa:
  300.         getsym();
  301.         temp1 = head;
  302.         temp2 = tail;
  303.         head = tail = NULL;
  304.         decl1();
  305.         needpunc(closepa);
  306.         temp3 = head;
  307.         temp4 = tail;
  308.         head = temp1;
  309.         tail = temp2;
  310.         decl2();
  311.         if (temp4 != NULL) {
  312.             temp4->btp = head;
  313.             if (temp4->type == bt_pointer && temp4->val_flag != 0
  314.                 && head != NULL)
  315.                 temp4->size *= head->size;
  316.         }
  317.         head = temp3;
  318.         break;
  319.     default:
  320.         decl2();
  321.         break;
  322.     }
  323. }
  324.  
  325. void
  326. decl2()
  327. {
  328.     TYP            *temp1;
  329.  
  330.     switch (lastst) {
  331.     case openbr:
  332.         getsym();
  333.         temp1 = maketype(bt_pointer, 4);
  334.         temp1->val_flag = 1;
  335.         if (lastst == closebr) {
  336.             temp1->size = 0;
  337.             temp1->val_flag = 1;
  338.             getsym();
  339.         }
  340.         else {
  341.             temp1->size = intexpr();
  342.             needpunc(closebr);
  343.         }
  344.         decl2();
  345.         if (head != NULL) {
  346.             temp1->size *= head->size;
  347.         }
  348.         temp1->btp = head;
  349.         head = temp1;
  350.         if (tail == NULL)
  351.             tail = head;
  352.         break;
  353.     case openpa:
  354.         getsym();
  355.         temp1 = maketype(bt_func, 0);
  356.         temp1->val_flag = 1;
  357.         temp1->btp = head;
  358.         head = temp1;
  359.         if (lastst == closepa) {
  360.             getsym();
  361.             if (lastst == begin)
  362.                 temp1->type = bt_ifunc;
  363.         }
  364.         else
  365.             temp1->type = bt_ifunc;
  366.         break;
  367.     }
  368. }
  369.  
  370. int
  371. alignment(tp)
  372.     TYP            *tp;
  373. {
  374.     if (tp == NULL) {
  375.         fprintf( stderr, "DIAG -- NULL argument to alignment.\n" );
  376.         return (AL_CHAR);
  377.     }
  378.     switch (tp->type) {
  379.     case bt_char:
  380.     case bt_uchar:
  381.         return AL_CHAR;
  382.     case bt_short:
  383.     case bt_ushort:
  384.         return AL_SHORT;
  385.     case bt_long:
  386.     case bt_unsigned:
  387.         return AL_LONG;
  388.     case bt_enum:
  389.         return AL_SHORT;
  390.     case bt_pointer:
  391.         if (tp->val_flag)
  392.             return alignment(tp->btp);
  393.         else
  394.             return AL_POINTER;
  395.     case bt_float:
  396.         return AL_FLOAT;
  397.     case bt_double:
  398.         return AL_DOUBLE;
  399.     case bt_struct:
  400.     case bt_union:
  401.         return AL_STRUCT;
  402.     default:
  403.         return AL_CHAR;
  404.     }
  405. }
  406.  
  407. int
  408. declare(table, al, ilc, ztype, ral)
  409.  
  410. /*
  411.  * process declarations of the form:
  412.  * 
  413.  * <type>  <decl>, <decl>...;
  414.  * 
  415.  * leaves the declarations in the symbol table pointed to by table and returns
  416.  * the number of bytes declared. al is the allocation type to assign, ilc is
  417.  * the initial location counter. if al is sc_member then no initialization
  418.  * will be processed. ztype should be bt_struct for normal and in structure
  419.  * declarations and sc_union for in union declarations.
  420.  * 
  421.  * If al == sc_auto && ral == sc_member then we are processing parameters
  422.  * 
  423.  */
  424.     TABLE          *table;
  425.     enum e_sc       al;
  426.     int             ilc;
  427.     enum e_bt       ztype;
  428.     enum e_sc       ral;
  429. {
  430.     SYM            *sp, *sp1;
  431.     TYP            *dhead, *tp2;
  432.     int             nbytes = 0, num;
  433.  
  434.     if (lastst == kw_typedef) {
  435.         getsym();
  436.         decl(table);
  437.         dhead = head;
  438.         for (;;) {
  439.             declid = NULL;
  440.             decl1();
  441.             if (declid != NULL) {
  442.                 sp = (SYM *) xalloc(sizeof(SYM));
  443.                 sp->name = declid;
  444.                 sp->storage_class = sc_type;
  445.                 sp->value.i = 0;
  446.                 tp2 = maketype(bt_typedef, 0);
  447.                 tp2->btp = head;
  448.                 tp2->size = head->size;
  449.                 head = tp2;
  450.                 sp->tp = head;
  451.                 insert(sp, table);
  452. #ifdef  GENERATE_DBX
  453.                 dbx_ident(sp);
  454. #endif
  455.             }
  456.             if (lastst != comma)
  457.                 break;
  458.             needpunc(comma);
  459.             head = dhead;
  460.         }
  461.         needpunc(semicolon);
  462.         return (0);
  463.     }
  464.  
  465.     decl(table);
  466.     dhead = head;
  467.     for (;;) {
  468.         declid = NULL;
  469.         decl1();
  470.  
  471.         if (head == NULL) {
  472.             error( ERR_SYNTAX, "unknown type" );
  473.             break;
  474.         }
  475.         
  476.         if (ral == sc_member && al == sc_auto || ral == sc_parameter) {
  477.             ral = sc_parameter; /* Parameter Decl */
  478.             switch (head->type) {
  479.             case bt_uchar:
  480.             case bt_ushort:
  481.                 head = tail = maketype(bt_unsigned, 4);
  482.                 break;
  483.             case bt_char:
  484.             case bt_short:
  485.             case bt_enum:
  486.                 head = tail = maketype(bt_long, 4);
  487.                 break;
  488.             case bt_float:
  489.                 head = tail = maketype(bt_double, 8);
  490.                 break;
  491.             }
  492.             if (head->size == 0 || head->type == bt_pointer)
  493.                 head->val_flag = 0; /* Make *argv[] work */
  494.         }
  495.  
  496.         if (declid != NULL) {   /* otherwise just struct tag... */
  497.             sp = (SYM *) xalloc(sizeof(SYM));
  498.             sp->name = declid;
  499.             sp->storage_class = al;
  500.             sp->storage_type = ral;
  501.             sp->next = NULL;
  502.             sp->value.i = 0;
  503.             num = alignment(head);
  504.             if ((ilc + nbytes) % num != 0) {
  505.                 if (al != sc_member && al != sc_external && al != sc_auto) {
  506.                     dseg();
  507.                     genalignment(num);
  508.                 }
  509.                 while ((ilc + nbytes) % num != 0)
  510.                     ++nbytes;
  511.             }
  512.             sp->tp = head;
  513.             if (al == sc_static) {
  514.                 sp->value.i = nextlabel++;
  515.                 if (sp->tp->type == bt_ifunc) {
  516.                     sp1 = search(sp->name, table->head);
  517.                     if (sp1 != NULL && sp1->tp->type == bt_func) {
  518.                         sp->value.i = sp1->value.i;
  519.                     }
  520.                 }
  521.             }
  522.             else if (ztype == bt_union)
  523.                 sp->value.i = ilc;
  524.             else if (al != sc_auto)
  525.                 sp->value.i = ilc + nbytes;
  526.             else
  527.                 sp->value.i = -(ilc + nbytes + head->size);
  528.  
  529.             if (sp->tp->type == bt_func) {
  530.                 if (sp->storage_class == sc_global || 
  531.                     sp->storage_class == sc_auto) {
  532.                     sp->storage_class = sc_external;
  533.                 }
  534.             }
  535.  
  536.             if (ztype == bt_union)
  537.                 nbytes = imax(nbytes, sp->tp->size);
  538.             else if (al != sc_external)
  539.                 nbytes += sp->tp->size;
  540.  
  541.             if (sp->tp->type != bt_ifunc) 
  542.                 insert(sp, table);
  543.             else {
  544.                 sp1 = search(sp->name, table->head);
  545.                 if (sp1 == NULL || sp1->tp->type != bt_func) 
  546.                     insert(sp, table);
  547.                 else {
  548. #if 0
  549.                     if ((sp1->storage_class != sp->storage_class) ||
  550.                         (sp1->storage_type  != sp->storage_type ))
  551.                         error( ERR_SYNTAX, "storage class is not consistant" );
  552. #endif
  553.                     sp1->tp = sp->tp;
  554.                     sp1->storage_class = sp->storage_class;
  555.                     sp1->storage_type  = sp->storage_type;
  556.                     sp1->value.i = sp->value.i;
  557.                     sp = sp1;
  558.                 }
  559.             }
  560.  
  561.             if (al == sc_auto)
  562.                 doinitauto(sp);
  563.  
  564.             if ((al == sc_global || al == sc_static) &&
  565.                 (sp->tp->type != bt_func && sp->tp->type != bt_ifunc))
  566.                 doinit(sp);
  567.  
  568. #ifdef  GENERATE_DBX
  569.             if (ral != sc_parameter)
  570.                 dbx_ident(sp);
  571. #endif
  572.  
  573.             if (sp->tp->type == bt_ifunc) { /* function body follows */
  574.                 funcbody(sp);
  575.                 return nbytes;
  576.             }
  577.         }
  578.  
  579.         if (lastst == semicolon)
  580.             break;
  581.  
  582.         needpunc(comma);
  583.  
  584.         if (!declbegin(table, lastst))
  585.             break;
  586.         head = dhead;
  587.     }
  588.     getsym();
  589.     return nbytes;
  590. }
  591.  
  592. int
  593. declbegin(table, st)
  594.     TABLE          *table;
  595.     enum e_sym      st;
  596. {
  597.     TYP            *tp;
  598.  
  599.     tp = istypedef(table);
  600.  
  601.     if (tp != NULL)
  602.         return (FALSE);
  603.  
  604.     return st == star || st == id || st == openpa || st == openbr;
  605. }
  606.  
  607. void
  608. declenum(table)
  609.     TABLE          *table;
  610. {
  611.     SYM            *sp;
  612.  
  613.     ++global_flag;
  614.  
  615.     if (lastst == id) {
  616.         if ((sp = search(lastid, tagtable.head)) == NULL) {
  617.             sp = (SYM *) xalloc(sizeof(SYM));
  618.             bzero( sp, sizeof(SYM));
  619.             sp->storage_class = sc_type;
  620.             sp->name = litlate(lastid);
  621.             sp->tp = (TYP *) xalloc(sizeof(TYP));
  622.             sp->tp->type = bt_enum;
  623.             sp->tp->size = 2;
  624.             sp->tp->lst.head = NULL;
  625.             sp->tp->lst.tail = NULL;
  626.             sp->tp->btp = NULL;
  627.             sp->tp->sname = sp->name;
  628.             getsym();
  629.             if (lastst != begin)
  630.                 error(ERR_INCOMPLETE, NULL);
  631.             else {
  632.                 insert(sp, &tagtable);
  633.                 getsym();
  634.                 enumbody(sp, table);
  635. #ifdef  GENERATE_DBX
  636.                 dbx_ident(sp);
  637. #endif
  638.             }
  639.         }
  640.         else
  641.             getsym();
  642.         head = sp->tp;
  643.     }
  644.     else {
  645.         sp = (SYM *) xalloc(sizeof(SYM));
  646.         bzero( sp, sizeof(SYM) );
  647.         sp->storage_class = sc_type;
  648.         sp->name = NULL;
  649.         sp->tp = (TYP *) xalloc(sizeof(TYP));
  650.         sp->tp->type = bt_short;
  651.         sp->tp->size = 2;
  652.         sp->tp->lst.head = NULL;
  653.         sp->tp->lst.tail = NULL;
  654.         sp->tp->btp = NULL;
  655.         sp->tp->sname = NULL;
  656.         if (lastst != begin)
  657.             error(ERR_INCOMPLETE, NULL);
  658.         else {
  659.             getsym();
  660.             enumbody(sp, table);
  661. #ifdef  GENERATE_DBX
  662.             dbx_ident(sp);
  663. #endif
  664.         }
  665.         head = sp->tp;
  666.     }
  667.  
  668.     --global_flag;
  669. }
  670.  
  671. void
  672. enumbody(sym_sp, table)
  673.     SYM            *sym_sp;
  674.     TABLE          *table;
  675. {
  676.     int             evalue;
  677.     SYM            *sp, *sp2;
  678.  
  679.     evalue = 0;
  680.     while (lastst == id) {
  681.         sp = (SYM *) xalloc(sizeof(SYM));
  682.         sp->name = litlate(lastid);
  683.         sp->storage_class = sc_const;
  684.         sp->value.i = evalue++;
  685.         sp->tp = &stdconst;
  686.         insert(sp, table);
  687.         sp2 = copysym(sp);
  688.         insert(sp2, &(sym_sp->tp->lst));
  689.         getsym();   /* Skip the identifier */
  690.         if (lastst == assign) {
  691.             getsym();
  692.             sp->value.i = sp2->value.i = intexpr();
  693.             evalue = (sp->value.i) + 1;
  694.         }
  695.         if (lastst == comma)
  696.             getsym();
  697.         else if (lastst != end)
  698.             break;
  699.     }
  700.     needpunc(end);
  701. }
  702.  
  703. void
  704. declstruct(ztype)
  705.  
  706. /*
  707.  * declare a structure or union type. ztype should be either bt_struct or
  708.  * bt_union.
  709.  */
  710.     enum e_bt       ztype;
  711. {
  712.     SYM            *sp;
  713.  
  714.     ++global_flag;
  715.  
  716.     if (lastst == id) {
  717.         if ((sp = search(lastid, tagtable.head)) == NULL) {
  718.             sp = (SYM *) xalloc(sizeof(SYM));
  719.             bzero(sp, sizeof(SYM));
  720.             sp->name = litlate(lastid);
  721.             sp->storage_class = sc_type;
  722.             sp->next = NULL;
  723.             sp->tp = (TYP *) xalloc(sizeof(TYP));
  724.             sp->tp->type = ztype;
  725.             sp->tp->lst.head = NULL;
  726.             sp->tp->sname = sp->name;
  727.             getsym();
  728.             if (lastst == star || lastst == semicolon) {
  729.                 sp->tp->size = 0;   /* Forward declaration */
  730.                 insert(sp, &tagtable);
  731.             }
  732.             else if (lastst != begin)
  733.                 error(ERR_INCOMPLETE, NULL);
  734.             else {
  735.                 insert(sp, &tagtable);
  736.                 getsym();
  737.                 structbody(sp->tp, ztype);
  738. #ifdef  GENERATE_DBX
  739.                 dbx_ident(sp);
  740. #endif
  741.             }
  742.         }
  743.         else {
  744.             if (sp->tp->size != 0)
  745.                 getsym();
  746.             else {
  747.                 sp->tp->type = ztype;
  748.                 sp->tp->lst.head = NULL;
  749.                 sp->storage_class = sc_type;
  750.                 sp->tp->sname = sp->name;
  751.                 getsym();
  752.                 if (lastst == star || lastst == semicolon)
  753.                     sp->tp->size = 0;   /* Forward declaration */
  754.                 else if (lastst != begin)
  755.                     error(ERR_INCOMPLETE, NULL);
  756.                 else {
  757.                     getsym();
  758.                     structbody(sp->tp, ztype);
  759. #ifdef  GENERATE_DBX
  760.                     dbx_ident(sp);
  761. #endif
  762.                 }
  763.             }
  764.         }
  765.         head = sp->tp;
  766.     }
  767.     else {
  768.         sp = (SYM *) xalloc(sizeof(SYM));
  769.         sp->name = NULL;
  770.         sp->storage_class = sc_type;
  771.         sp->next = NULL;
  772.         sp->tp = (TYP *) xalloc(sizeof(TYP));
  773.         sp->tp->type = ztype;
  774.         sp->tp->sname = NULL;
  775.         sp->tp->lst.head = NULL;
  776.         sp->tp->lst.tail = NULL;
  777.         if (lastst != begin)
  778.             error(ERR_INCOMPLETE, NULL);
  779.         else {
  780.             getsym();
  781.             structbody(sp->tp, ztype);
  782. #ifdef  GENERATE_DBX
  783.             dbx_ident(sp);
  784. #endif
  785.         }
  786.         head = sp->tp;
  787.     }
  788.  
  789.     --global_flag;
  790. }
  791.  
  792. void
  793. structbody(tp, ztype)
  794.     TYP            *tp;
  795.     enum e_bt       ztype;
  796. {
  797.     int             slc;
  798.  
  799.     if (tp == NULL) {
  800.         fprintf( stderr, "DIAG -- NULL argument to structbody.\n" );
  801.         return;
  802.     }
  803.     slc = 0;
  804.     tp->val_flag = 1;
  805.     tp->size = -1;
  806.     while (lastst != end) {
  807.         if (ztype == bt_struct)
  808.             slc += declare(&(tp->lst), sc_member, slc, ztype, sc_member);
  809.         else
  810.             slc = imax(slc, declare(&tp->lst, sc_member, 0, ztype, sc_member));
  811.     }
  812.  
  813.     /*
  814.      * The size of a structure includes padding, which should be a power
  815.      * of 2 (for the implementation below
  816.      */
  817.     tp->size = (slc + AL_STRUCT - 1) & ~(AL_STRUCT - 1);
  818.     getsym();
  819. }
  820.  
  821. void
  822. dodecl(defclass)
  823.     enum e_sc       defclass;
  824. {
  825.     TYP            *tp;
  826.  
  827.     for (;;) {
  828.         if (fatal)
  829.             return;
  830.         switch (lastst) {
  831.         case asmconst:
  832.             addauto(asmstmt());
  833.             break;
  834.         case kw_typedef:
  835.             if (defclass == sc_global)
  836.                 declare(&gsyms, sc_global, lc_static, bt_struct, defclass);
  837.             else if (defclass == sc_auto)
  838.                 declare(&lsyms, sc_auto, lc_auto, bt_struct, defclass);
  839.             else
  840.                 declare(&lsyms, sc_auto, 0, bt_struct, defclass);
  841.             break;
  842.         case kw_register:
  843.             getsym();
  844.             if (defclass != sc_auto && defclass != sc_member)
  845.                 error(ERR_ILLCLASS, NULL);
  846.             goto do_decl;
  847.         case kw_auto:
  848.             getsym();
  849.             if (defclass != sc_auto && defclass != sc_member)
  850.                 error(ERR_ILLCLASS, NULL);
  851.             goto do_decl;
  852.         case kw_const:
  853.             getsym();
  854.             if (defclass != sc_auto && defclass != sc_member)
  855.                 error(ERR_ILLCLASS, NULL);
  856.             goto do_decl;
  857.         case kw_volatile:
  858.             getsym();
  859.             if (defclass != sc_auto && defclass != sc_member)
  860.                 error(ERR_ILLCLASS, NULL);
  861.             goto do_decl;
  862.         case id:
  863.             if ((tp = istypedef(&lsyms)) != NULL)
  864.                 goto do_decl;
  865.             if ((tp = istypedef(&gsyms)) != NULL)
  866.                 goto do_decl;
  867.             tp = tp;
  868.             if (defclass == sc_auto)
  869.                 return;
  870.  
  871.             /* else fall through to declare    */
  872.         case kw_char:
  873.         case kw_int:
  874.         case kw_short:
  875.         case kw_unsigned:
  876.         case kw_long:
  877.         case kw_struct:
  878.         case kw_union:
  879.         case kw_enum:
  880.         case kw_void:
  881.         case kw_float:
  882.         case kw_double:
  883.     do_decl:    if (defclass == sc_global)
  884.                 lc_static +=
  885.                     declare(&gsyms, sc_global, lc_static, bt_struct, defclass);
  886.             else if (defclass == sc_auto)
  887.                 lc_auto +=
  888.                     declare(&lsyms, sc_auto, lc_auto, bt_struct, defclass);
  889.             else
  890.                 declare(&lsyms, sc_auto, 0, bt_struct, defclass);
  891.             break;
  892.         case kw_static:
  893.             getsym();
  894.             if (defclass == sc_member)
  895.                 error(ERR_ILLCLASS, NULL);
  896.             if (defclass == sc_auto)
  897.                 lc_static +=
  898.                     declare(&lsyms, sc_static, lc_static, bt_struct, defclass);
  899.             else
  900.                 lc_static +=
  901.                     declare(&gsyms, sc_static, lc_static, bt_struct, defclass);
  902.             break;
  903.         case kw_extern:
  904.             getsym();
  905.             if (defclass == sc_member)
  906.                 error(ERR_ILLCLASS, NULL);
  907.             ++global_flag;
  908.             declare(&gsyms, sc_external, 0, bt_struct, defclass);
  909.             --global_flag;
  910.             break;
  911.         default:
  912.             return;
  913.         }
  914.     }
  915. }
  916.  
  917. void
  918. compile()
  919.  
  920. /*
  921.  * main compiler routine. this routine parses all of the declarations using
  922.  * declare which will call funcbody as functions are encountered.
  923.  */
  924. {
  925.     if (autohead != NULL) {
  926.         genstmt(autohead);
  927.         flush_peep();
  928.         autohead = autotail = NULL;
  929.     }
  930.  
  931. #ifdef  GENERATE_DBX
  932.     dbx_init();
  933. #endif
  934.  
  935.     while (lastst != eof) {
  936.         if (fatal)
  937.             return;
  938.         dodecl(sc_global);
  939.         if (lastst != eof)
  940.             getsym();
  941.     }
  942.  
  943.     if (autohead != NULL) {
  944.         genstmt(autohead);
  945.         flush_peep();
  946.         autohead = autotail = NULL;
  947.     }
  948.     dumplits();
  949. }
  950.  
  951. int
  952. declproto(table)
  953.     TABLE          *table;  /* table is the argument table for the function */
  954.  
  955. /*
  956.  * process declarations of the form:
  957.  * 
  958.  * ( <type> [<decl>] [,<type> [<decl>]] ... )
  959.  * 
  960.  */
  961.  
  962. {
  963.     SYM            *sp, *sp2;
  964.     int             nbytes;
  965.  
  966.     nbytes = 8;     /* return block */
  967.     global_flag++;
  968.  
  969.     for (;;) {
  970.         declid = NULL;
  971.  
  972.         decl(&lsyms);   /* Set the base type of the variable */
  973.         decl1();
  974.  
  975.         /* Make *argv[] work */
  976.  
  977.         if (head->size == 0 || head->type == bt_pointer)
  978.             head->val_flag = 0;
  979.  
  980.         sp = (SYM *) xalloc(sizeof(SYM));
  981.         sp->name = declid;
  982.         sp->storage_class = sc_auto;
  983.         sp->storage_type = sc_proto;
  984.         sp->next = NULL;
  985.  
  986.         sp->value.i = nbytes;
  987.         sp->tp = head;
  988.  
  989.         if (sp->tp->size >= 4)
  990.             nbytes += sp->tp->size;
  991.         else {
  992.             if (sp->tp->size == 1)  /* byte reference */
  993.                 sp->tp->size += 1;
  994.             nbytes += 2;
  995.         }
  996.  
  997.         sp2 = copysym(sp);
  998.  
  999.         switch (sp->tp->type) {
  1000.         case bt_uchar:
  1001.         case bt_ushort:
  1002.             sp->tp = maketype(bt_unsigned, 4);
  1003.             break;
  1004.         case bt_char:
  1005.         case bt_short:
  1006.         case bt_enum:
  1007.             sp->tp = maketype(bt_long, 4);
  1008.             break;
  1009.         case bt_float:
  1010.             sp->tp = maketype(bt_double, 8);
  1011.             break;
  1012.         }
  1013.  
  1014.         insert(sp, &lsyms); /* Insert into the local symbols */
  1015.         insert(sp2, table); /* Insert into the function list */
  1016.  
  1017.         if (lastst != comma)
  1018.             break;
  1019.         getsym();
  1020.     }
  1021.     if (lastst == ellipsis) {
  1022.         getsym();
  1023.         if (lastst != closepa)
  1024.             error(ERR_SYNTAX, NULL);
  1025.     }
  1026.  
  1027.     global_flag--;
  1028.     return nbytes;
  1029. }
  1030.